﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
namespace XRPGLibrary.SpriteClasses
{
    public enum AnimationKey { Down, Left, Right, Up, AttackRight,AttackLeft }//动态精灵所对应的方向
    public class Animation : ICloneable
    {
        #region Field Region
        Rectangle[] frames;//精灵帧的矩形框
        int framesPerSecond;//帧频率变量
        TimeSpan frameLength;//相邻帧之间的时间间隔
        TimeSpan frameTimer;
        int currentFrame;//当前帧的索引
        int frameWidth;//帧的宽度和高度
        int frameHeight;
        #endregion
        #region Property Region
        public int FramesPerSecond
        {
            get { return framesPerSecond; }
            set
            {
                //对帧频的限定，要保持在1到60之间
                if (value < 1)
                    framesPerSecond = 1;
                else if (value > 60)
                    framesPerSecond = 60;
                else
                    framesPerSecond = value;
                frameLength = TimeSpan.FromSeconds(1 / (double)framesPerSecond);//初始化帧之间的时间间隔
            }
        }
        public Rectangle CurrentFrameRect
        {
            get { return frames[currentFrame]; }
        }
        public int CurrentFrame
        {
            get { return currentFrame; }
            set
            {
                currentFrame = (int)MathHelper.Clamp(value, 0, frames.Length - 1);//限定当前帧索引的范围
            }
        }
        public int FrameWidth
        {
            get { return frameWidth; }
        }
        public int FrameHeight
        {
            get { return frameHeight; }
        }
        #endregion
        #region Constructor Region其中xOffset和yOffset的作用是实现在不同行不同列上截取图片
        public Animation(int frameCount, int frameWidth, int frameHeight, int xOffset, int yOffset)
        {
            frames = new Rectangle[frameCount];//初始化该动画的帧集
            this.frameWidth = frameWidth;
            this.frameHeight = frameHeight;
            for (int i = 0; i < frameCount; i++)//根据选定的行来实例化动画中的帧图
            {
                frames[i] = new Rectangle(
                xOffset + (frameWidth * i),
                yOffset,
                frameWidth,
                frameHeight);
            }
            FramesPerSecond = 5;//设置帧频
            Reset();
        }
        //直接用动画来初始化
        private Animation(Animation animation)
        {
            this.frames = animation.frames;
            FramesPerSecond = 5;
        }
        #endregion
        #region Method Region动画中帧的更新
        public void Update(GameTime gameTime)
        {
            frameTimer += gameTime.ElapsedGameTime;
            if (frameTimer >= frameLength)
            {
                frameTimer = TimeSpan.Zero;
                currentFrame = (currentFrame + 1) % frames.Length;//当时间间隔超过设定值时间，翻到下一帧
            }
        }
        public void Reset()
        {
            currentFrame = 0;//重置当前的帧索引和时间间隔的值
            frameTimer = TimeSpan.Zero;
        }
        #endregion
        #region Interface Method Region
        public object Clone()
        {
            Animation animationClone = new Animation(this);
            animationClone.frameWidth = this.frameWidth;
            animationClone.frameHeight = this.frameHeight;
            animationClone.Reset();
            return animationClone;
        }
        #endregion
    }
}